﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Twipler.App;
using Twipler.Data.Access;
using Twipler.Data.Entity.Theme;
using Twipler.Data.Entity.Twipler;
using Twipler.WebApp;

using NHibernate.Criterion;
using Twipler.Data.Session;

namespace Settings.Theme
{
    [RequireUser(VisitorRole.ThemeCreator)]
    public partial class DesignList : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UpdateDisplay();

                int designId;
                if ((Request.QueryString["edit"] != null) && (int.TryParse(Request.QueryString["edit"], out designId)))
                {
                    editDesign.Entity = Factory.Design.GetById(designId, false);
                    editDesign.UpdateDisplay();
                    mpeEditEntity.Show();
                }
            }
        }

        private void UpdateDisplay()
        {
            if (Current.Visitor.HasRole(VisitorRole.Admin))
            {
                grdDesigns.DataSource = Factory.Design.GetAll();
            }
            else
            {
                grdDesigns.DataSource = Factory.Design.GetByCriteria(
                        Expression.Or(
                            Expression.Eq("State", (int)DesignState.Published),                            
                            Expression.And(
                                Expression.Eq("CreatedBy", Current.Visitor),
                                Expression.Eq("State", (int)DesignState.Private)))
                    );
            }
            grdDesigns.DataBind();
        }

        protected void editDesign_Update()
        {
            UpdateDisplay();
        }
        
        protected void grdDesign_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Button btnDelete = (Button)e.Item.FindControl("btnDelete");

                if (((Design)e.Item.DataItem).UseCount > 0)
                {
                    btnDelete.CssClass = "greybutton";
                    btnDelete.Enabled = false;
                }
                else
                {
                    btnDelete.Visible = ((Design)e.Item.DataItem).UserCanModify();
                    btnDelete.Attributes.Add("onclick", "return window.confirm('Confirm deletion of container');");
                }
            }
        }

        protected void grdDesigns_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            int designId = int.Parse(e.Item.Cells[0].Text);
            Design design;

            switch (e.CommandName)
            {
                case "View":
                    design = Factory.Design.GetById(designId, false);
                    if (design.UserCanModify() == false)
                        Response.Redirect(QueryStringHash.Protect("~/settings/theme/containerlist.aspx?designid=" + designId + "&ro=t"));
                    else
                        Response.Redirect(QueryStringHash.Protect("~/settings/theme/containerlist.aspx?designid=" + designId));
                    break;

                case "Edit":
                    editDesign.Entity = Factory.Design.GetById(designId, true);
                    editDesign.UpdateDisplay();
                    mpeEditEntity.Show();
                    break;

                case "Delete":
                    design = Factory.Design.GetById(designId, false);
                    if (design.UseCount == 0)
                        design.Delete();

                    UpdateDisplay();
                    break;
            }

        }
    }
}